package xj.graph2d.app.prop;

import java.awt.Container;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

import xj.graph2d.Command;
import xj.graph2d.DrawAttr;
import xj.graph2d.DrawCanvas;
import xj.graph2d.DrawShape;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;
import xj.graph2d.app.DrawApp;
import xj.graph2d.command.SetAttrCommand;

public class LineWidthSelectionListener extends AttrListener implements
    ActionListener {

  public void init(LineWidthIcon icon) {
    this.icon = icon;
    SimpleDrawDocument doc = main.getDoc();
    SimpleDrawView view = main.getView();
    attr = view.getDrawAttr();
  }

  @Override
  protected void init() {
    SimpleDrawDocument doc = main.getDoc();
    SimpleDrawView view = main.getView();
    attr = view.getDrawAttr();
    Object ic = DrawApp.getIcon("lineWidthIcon");
    if (ic instanceof LineWidthIcon) {
      icon = (LineWidthIcon) ic;
    }
  }

  public void actionPerformed(ActionEvent e) {
    SimpleDrawDocument doc = main.getDoc();
    SimpleDrawView view = main.getView();
    attr = view.getDrawAttr();
    if (attr != null) {
      if (popup == null) {
	initPopup();
      } else {
	resetPopup();
      }

      Object source = e.getSource();
      if (source instanceof AbstractButton) {
	comp = (AbstractButton) source;
      }
      if (popup != null && comp != null) {
	int h = comp.getHeight();
	popup.show(comp, 0, h);
      }

      if (comp != null) {
	comp.setSelected(false);
      }
    }
  }

  protected void postAction() {
  }

  class LineWidthAction implements ActionListener {

    public void actionPerformed(ActionEvent e) {
      Object source = e.getSource();
      if (source instanceof JButton) {
	JButton button = (JButton) source;
	Container p = button.getParent();
	p.setVisible(false);
	LineWidthIcon ic = (LineWidthIcon) button.getIcon();
	int w = ic.getWidth();
	int s = ic.getStyle();
	if (icon != null && comp != null) {
	  icon.setWidth(w);
	  icon.setStyle(s);
	  comp.repaint();
	}

	if (updateSystemAttr) {
	  // update system attribute
	  attr.setPenSize(w);
	  attr.setLineStyle(s);
	}

	// update current shape
	SimpleDrawDocument doc = main.getDoc();
	SimpleDrawView view = main.getView();
	if (doc != null) {
	  DrawShape curShape = doc.getSelectedShape();
	  if (curShape != null) {
	    DrawAttr curAttr = curShape.getDrawAttr();
	    if (curAttr != null) {
	      Command cmd = new SetAttrCommand(view, doc, curShape);
	      doc.addCommand(cmd);

	      curAttr.setPenSize(w);
	      curAttr.setLineStyle(s);
	      curShape.calculateBounds();
	      DrawCanvas canvas = view.getCanvas();
	      canvas.repaintShape(curShape);
	    }
	  }
	}
	postAction();
      }
    }
  }

  protected static int[] widthSelection = { 1, 2, 4, 6, 8, 10 };

  protected void initPopup() {
    ActionListener act = new LineWidthAction();

    popup = new JPopupMenu();
    JButton mi;
    int i;
    widthIcons = new LineWidthIcon[widthSelection.length];
    // int style = attr.getLineStyle();
    int style = icon.getStyle();
    for (i = 0; i < widthSelection.length; i++) {
      LineWidthIcon ic = new LineWidthIcon();
      ic.setWidth(widthSelection[i]);
      ic.setStyle(style);
      widthIcons[i] = ic;
      mi = new JButton(ic);
      mi.setMargin(new Insets(0, 0, 0, 0));
      mi.addActionListener(act);
      popup.add(mi);
    }

    popup.add(new JSeparator());

    styleIcons = new LineWidthIcon[5];
    // int width = attr.getPenSize();
    int width = icon.getWidth();
    for (i = 0; i < 5; i++) {
      LineWidthIcon ic = new LineWidthIcon();
      ic.setWidth(width);
      ic.setStyle(i);
      styleIcons[i] = ic;
      mi = new JButton(ic);
      mi.setMargin(new Insets(0, 0, 0, 0));
      mi.addActionListener(act);
      popup.add(mi);
    }

  }

  protected void resetPopup() {
    int i;
    // int style = attr.getLineStyle();
    int style = icon.getStyle();
    for (i = 0; i < widthIcons.length; i++) {
      LineWidthIcon ic = widthIcons[i];
      ic.setWidth(widthSelection[i]);
      ic.setStyle(style);
    }
    // int width = attr.getPenSize();
    int width = icon.getWidth();
    for (i = 0; i < styleIcons.length; i++) {
      LineWidthIcon ic = styleIcons[i];
      ic.setWidth(width);
      ic.setStyle(i);
    }

  }

  protected LineWidthIcon[] widthIcons;

  protected LineWidthIcon[] styleIcons;

  protected LineWidthIcon icon = null;

  protected DrawAttr attr = null;

  protected JPopupMenu popup = null;

  protected AbstractButton comp = null;

}
